#include "ds18b20.h"
#include "../../stdio/include/stdio.h"
#include "../gpio/bsp_gpio.h"
#include <stdio.h>

void DS18B20_Init(void) {
  IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER2_GPIO5_IO02, 0);
  IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER2_GPIO5_IO02, 0x70b0);
  gpio_pin_config_t config;
  config.direction = kGPIO_DigitalOutput;
  config.outputLogic = 1;
  config.interruptMode = kGPIO_NoIntmode;     
  gpio_init(GPIO5, 2, &config);
}

/**
 * @brief 采集温度
 *
 */
float DS18B20_Read_Temperature(void) {
  uint8_t res = 0;
  DS18B20_Write_Byte(0xcc);
  DS18B20_Write_Byte(0x44);
  res = DS18B20_Detect();
  if (res) {
    printf("失败 \r\n");
  }
  DQ_DELAY_US(200);
  DS18B20_Write_Byte(0xcc);
  DS18B20_Write_Byte(0xBE);

  uint8_t low = DS18B20_Read_Byte();
  uint8_t high = DS18B20_Read_Byte();

  printf("DS18B20_Read_Temperature  %X \r\n", low);
  printf("DS18B20_Read_Temperature %X \r\n", high);
  uint8_t integer = (low >> 4) | (high << 4);
  uint8_t decimal = low & 0xf;

  if ((high >> 7) == 1) {
    integer = ~integer;
    decimal = ((~decimal) & 0xf) + 1;
  }
  float temperature = integer + (((decimal >> 3) & 0x1) * 0.5) +
                      (((decimal >> 2) & 0x1) * 0.25) +
                      (((decimal >> 1) & 0x1) * 0.125) +
                      (((decimal >> 0) & 0x1) * 0.0625);

  if ((high >> 7) == 1) {
    temperature = -temperature;
  }
  printf(" temperature %f \r\n", temperature);
  return temperature;
}

/**
 * @brief 读取 rom code
 *
 * @param id 64位
 */
void DS18B20_Read_Id(uint8_t *id) {

  uint8_t i = 0;

  DS18B20_Write_Byte(0x33);

  for (; i < 8; i++) {
    *(id + i) = DS18B20_Read_Byte();
  }
}

/**
 * @brief 读一个字节
 *
 * @return uint8_t
 */
uint8_t DS18B20_Read_Byte(void) {
  uint8_t res = 0;
  int i = 0;
  for (; i < 8; i++) {
    res |= (DS18B20_Read_Bit() << i);
  }
  return res;
}

/**
 * @brief 写一个字节
 *
 * @param byte
 */
void DS18B20_Write_Byte(uint8_t byte) {
  int i = 0;
  for (; i < 8; i++) {
    DS18B20_Write_Bit((byte >> i) & 0x1);
  }
}

/**
 * @brief 读一位
 *
 * @return uint8_t
 */
uint8_t DS18B20_Read_Bit(void) {
  uint8_t res = 0; // 输出高5us

  DQ_OUT_MODE();
  DQ_OUTPUT_LEVEL(HIGH);
  DQ_DELAY_US(2);
  DQ_OUTPUT_LEVEL(LOW);
  DQ_DELAY_US(2);
  DQ_IN_MODE();
  DQ_DELAY_US(5);
  res = DQ_INPUT_LEVEL();
  DQ_DELAY_US(60);

  return res;
}

/**
 * @brief 写一位
 *
 * @param bit
 */
void DS18B20_Write_Bit(uint8_t bit) {
  if (bit) {
    DQ_OUT_MODE();
    DQ_OUTPUT_LEVEL(LOW);
    DQ_DELAY_US(5);
    DQ_IN_MODE();
    DQ_DELAY_US(60);
  } else {
    DQ_OUT_MODE();
    DQ_OUTPUT_LEVEL(LOW);
    DQ_DELAY_US(60);
    DQ_IN_MODE();
    DQ_DELAY_US(5);
  }
}

/**
 * @brief 查询设备在不在
 *
 * @return uint8_t 1--在  0--不在
 */
uint8_t DS18B20_Detect(void) {
  uint8_t res = 0;

  // 输出高1us
  DQ_OUTPUT_LEVEL(HIGH);
  DQ_DELAY_US(2);
  // 输出低480us
  DQ_OUTPUT_LEVEL(LOW);
  DQ_DELAY_US(480);
  // 释放
  DQ_IN_MODE();
  DQ_DELAY_US(30);
  // 输入
  DQ_DELAY_US(50);
  if (DQ_INPUT_LEVEL() == 0) {
    res = 1;
  }
  DQ_DELAY_US(400);
  return res;
}
